package com.bdqn.boot.yuanshen.controller;

import com.bdqn.boot.yuanshen.bean.Admin;
import com.bdqn.boot.yuanshen.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * @author fly
 * @version 1.0
 * @className AdminController
 * @description TODO
 * @date 2025/3/17 11:46
 */
@RestController
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Admin admin, HttpSession session) {
        Map<String, Object> resultMap = new HashMap<>();

        try {
            // 调用服务层的登录方法
            Admin admin1 = adminService.login(admin.getUsername(), admin.getPassword());
            if (admin1 != null) {
                // 登录成功
                resultMap.put("success", true);
                resultMap.put("msg", "登录成功");
                resultMap.put("admin", admin1);
                session.setAttribute("admin", admin1);
            } else {
                // 用户名或密码错误
                resultMap.put("success", false);
                resultMap.put("msg", "用户名或密码错误");
            }
        } catch (Exception e) {
            // 处理其他异常
            resultMap.put("success", false);
            resultMap.put("msg", "用户名或密码错误");
            // 记录异常日志，方便后续排查问题
            System.err.println("登录异常: " + e.getMessage());
            e.printStackTrace();
        }

        return resultMap;
    }

    @GetMapping("getVideosCount")
    public int getVideosCount() {
        return adminService.getVideosCount();
    }

    @GetMapping("getUsersCount")
    public int getUsersCount() {
        return adminService.getUsersCount();
    }

    @GetMapping("getUserBehavior")
    public int getUserBehavior() {
        return adminService.getUserBehavior();
    }
}
